package de.fret_save;

import java.awt.Dimension;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TreeSet;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

import de.fret_save.log.FS_Logger;


/**
 * FretSave Graphical User Interface
 * 
 * @author tze
 * 
 */
public class FS_Gui extends JFrame {

    /**
     * Konstruktor
     * 
     */
    public FS_Gui() {
        super("Fret Save");
        initComponents();
    }

    private void initComponents() {
        try {
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Exception ex) {
            FS_Logger.logWarn(this.getClass(), 
                    "Konnte 'Look and Feel' nicht setzen", ex);
        }
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(200, 200, 620, 300);
        setPreferredSize(new Dimension(620, 300));
        setResizable(false);
        getContentPane().setLayout(null);

        this.jGriffbrett = new JGriffbrett();
        this.jGriffbrett.setBackground(SystemColor.menu);
        this.jGriffbrett.setBounds(10, 10, 594, 200);
        this.jGriffbrett.setLayout(null);
        getContentPane().add(this.jGriffbrett);

        this.lblNewLabel = new JLabel("Akkord:");
        this.lblNewLabel.setBounds(10, 221, 46, 14);
        getContentPane().add(this.lblNewLabel);

        this.cmbChords = new JComboBox<String>();
        this.cmbChords.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                selectionChanged();
            }
        });
        this.cmbChords.setBounds(66, 218, 86, 20);
        getContentPane().add(this.cmbChords);
        FS_Data ds = FS_Data.getInstance();
        TreeSet<String> names = new TreeSet<String>(ds.getChordNames());
        for (String name : names) {
            this.cmbChords.addItem(name);
        }

        this.btExit = new JButton("Exit");
        this.btExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        this.btExit.setBounds(515, 238, 89, 23);
        getContentPane().add(this.btExit);

        this.btPrint = new JButton("Drucken");
        this.btPrint.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                print();
            }
        });
        this.btPrint.setBounds(420, 238, 89, 23);
        getContentPane().add(this.btPrint);
    }

    private static final long serialVersionUID = 1;
    private JGriffbrett jGriffbrett;
    private JLabel lblNewLabel;
    private JComboBox<String> cmbChords;
    private JButton btExit;
    private JButton btPrint;

    /**
     * Action bei Umselektierung der Combobox
     */
    private void selectionChanged() {
        String selected = this.cmbChords.getSelectedItem().toString();
        FS_Data ds = FS_Data.getInstance();
        Chord crd = ds.getChord(selected);
        if (null != crd)
            this.jGriffbrett.setChord(crd);
        else
            FS_Logger.logError(this.getClass(), "Akkord "
                    + selected + " konnte nicht gefunden werden", null);
    }

    private void print() {
        PrintHelper.printComponent(jGriffbrett);
    }
}
